home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / utils / refer-tools / ref2bib.awk < prev    next >
Text File  |  1994-11-13  |  13KB  |  507 lines

  1. #
  2. #    awk script to convert refer (or bib) format databases
  3. #    to BiBTeX format.
  4. #
  5. #    written by Peter King, Heriot-Watt University
  6. #    use freely, but dont claim that you wrote it
  7. #
  8. #    Generates keys using authors names and year (see %A entry )
  9. #    
  10. #    You may wish to alter treatment of key fields that are ignored
  11. #    such as %U %W %Y %K etc.
  12. #
  13. #  regular expressions should be sorted according to frequency 
  14. #  so that minimal tests are made
  15. #  From tests in a local data base the order given appears quite good
  16. #    2883 %A
  17. #    1813   blank lines
  18. #    1774 %T
  19. #    1764 %D
  20. #    1505 %P
  21. #    1347 %J
  22. #    1331 %V
  23. #    1201 %N
  24. #    773 .. continuation lines
  25. #    501 %C
  26. #    424 %I
  27. #    192 %B
  28. #    187 %E
  29. #    92 %S
  30. #    89 %R
  31. #    33 %X
  32. #    30 %K
  33. #    16 %O
  34. #    12 %any other % lines
  35. #
  36. BEGIN    {
  37.     for(i=1;i<=27;i++)
  38.         addkey[i] = substr(" abcdefghijklmnopqrstuvwxyz",i,1);
  39.     lkey = 3; # number of characters used from authors to make key
  40.     maxauthor = 3; # maximum number of authors to use in
  41.             # constructing key
  42.     rx = 1
  43.     }
  44.  
  45. /\\[*u0]/ || /\\d[^{]/|| /\\s[^s]/ {
  46.     err = 1
  47.     print "Non translated \\ symbol : Reference " rx > "ref2bib.errs"
  48.     print  $0 > "ref2bib.errs"
  49.     }
  50.  
  51. /^%A/    {
  52.     if (A==0) keys="";
  53.     A ++; lastx = "A"; 
  54.     authors[A] = substr($0,4)
  55.     if(A> maxauthor) next
  56.     ic = 0
  57.     lc = 1
  58.     while(ic < lkey && lc <= length($NF) ){
  59.         kc = substr( $NF, lc, 1)
  60.         if ( kc ~ /[a-zA-Z]/ ){
  61.             keys = keys kc
  62.             ic++
  63.             if(ic==lkey) next
  64.             }
  65.         else if ( kc == "\\" ) lc ++;
  66.         lc ++;
  67.         }
  68.     next
  69.     }
  70.  
  71. /^$/    {
  72.     if(NR==pr+1){
  73.         }
  74.     else {
  75.     refs ++
  76.     # if FILENAME != prevname then new file
  77.     acnt[A]++;if(A>MaxA)MaxA=A;
  78.     if(T==0)print "No title : Reference "refs" "keys > "ref2bib.errs"
  79.     if(A==0)print "No author : Reference "refs" "keys > "ref2bib.errs"
  80.     if(D==0)print "No date : Reference "refs" "keys > "ref2bib.errs"
  81.     if( (!T)||(!A)||(!D))err=1;
  82.     # classify the reference
  83.     if(J){
  84.         #journal or conference
  85.         if(B||E||R)print "Journal & book?: Reference "refs" "keys > "ref2bib.errs"
  86.         if(C||I) {conf++
  87.             type = "INPROCEEDINGS"
  88.             }
  89.         else{
  90.             jour ++;
  91.             type = "ARTICLE"
  92.             }
  93.         if(!P) print "No page nos.? : Reference "refs" "keys > "ref2bib.errs"
  94.         if( B||E||R||(!P))err=1
  95.         if(err){
  96.             print "Journal reference in error" > "ref2bib.errs"
  97.             }
  98.         }
  99.     else
  100.     if(B){
  101.         # article in book
  102.         type = "INCOLLECTION"
  103.         bookart++
  104.         if(N||R||(!E)||(!I)||(!C)||(!P)||(V&&(!S)))err=1
  105.         if(!E) print "No editor? Reference "refs" " keys > "ref2bib.errs"
  106.         if(!I) print "No publisher? Reference "refs" " keys > "ref2bib.errs"
  107.         if(!C) print "No city? Reference "refs" " keys > "ref2bib.errs"
  108.         if(!P) print "No page nos.? Reference "refs" " keys > "ref2bib.errs"
  109.         if(V&&(!S))print "Volume but no Series Reference "refs" " keys > "ref2bib.errs"
  110.         if(N)print "Issue no.? Reference "refs" " keys > "ref2bib.errs"
  111.         if(R)print "Report? Reference "refs" " keys > "ref2bib.errs"
  112.         if(err){
  113.             print "Book reference in error" > "ref2bib.errs"
  114.             }
  115.         }
  116.     else if(R){
  117.         #report
  118.         type = "TECHREPORT"
  119.         reps++
  120.         if(E||N)err=1
  121.         if(N)print "Issue no.? Reference "refs" " keys > "ref2bib.errs"
  122.         if(E) print "Editor? Reference "refs" " keys > "ref2bib.errs"
  123.         if(err){
  124.             print "Report reference in error" > "ref2bib.errs"
  125.             }
  126.         }
  127.     else if(I){
  128.         wholebook ++
  129.         type = "BOOK"
  130.         # book
  131.         if(N||R||E||(!C)||(V&&(!S)))err=1
  132.         if(!C) print "No city? Reference "refs" " keys > "ref2bib.errs"
  133.         if(N)print "Issue no.? Reference "refs" " keys > "ref2bib.errs"
  134.         if(E)print "Editor? Reference "refs" " keys > "ref2bib.errs"
  135.         if(V&&(!S))print "Volume but no Series Reference "refs" " keys > "ref2bib.errs"
  136.         if(err){
  137.             print "Book reference in error" > "ref2bib.errs"
  138.             }
  139.         }
  140.     else {
  141.         unclass ++
  142.         type = "MISC"
  143.         err=1
  144.             print "Unclassified reference in error" > "ref2bib.errs"
  145.         }
  146.  
  147.     # generate date
  148.     ndate = split(date,df)
  149.     if ( ndate > 2) print " Funny date " date > "ref2bib.errs"
  150.     if (ndate == 1 ) { df[2] = df[1]; df[1] = ""; }
  151.  
  152.  
  153.     # generate key
  154.     if(keys == "") keys = "ANON"
  155.     keys = keys substr(df[2],3,2)
  156.     if(keyused[keys] >=1) {
  157.         key_suffix = keyused[keys]++;
  158.         keys = keys addkey[key_suffix];
  159.         }
  160.     else keyused[keys] = 1
  161.     if (err)  {
  162.             print "Key: " keys > "ref2bib.errs"
  163.             if(A) for (i=1;i<=A;i++)
  164.                 print "%A " authors[i] > "ref2bib.errs"
  165.             if(T) print "%T " title > "ref2bib.errs"
  166.             if(J) print "%J "journal > "ref2bib.errs"
  167.             if(B) print "%B "book > "ref2bib.errs"
  168.             if(V) print "%V "volume > "ref2bib.errs"
  169.             if(N) print "%N "number > "ref2bib.errs"
  170.             if(I) print "%I "publisher > "ref2bib.errs"
  171.             if(C) print "%C "city > "ref2bib.errs"
  172.             if(E) for (i=1;i<=E;i++)print "%E "editor[i] > "ref2bib.errs"
  173.             if(S) print "%S "series > "ref2bib.errs"
  174.             if(P) print "%P "pages > "ref2bib.errs"
  175.             if(R) print "%R "report > "ref2bib.errs"
  176.             if(D) print "%D "date > "ref2bib.errs"
  177.             if(O) print "%O "other > "ref2bib.errs"
  178.             print "" > "ref2bib.errs"
  179.         }
  180.  
  181.     if(T){
  182.     twc = split(title,z)
  183.     title = z[1]; lt = length(z[1]);
  184.     for(i=2;i<=twc;i++) {
  185.         if(lt +length(z[i]) >= 55) {sc = "\n\t\t";lt = 0;}
  186.         else sc = " ";
  187.         title = title sc z[i]
  188.         lt += length(z[i]) + 1
  189.         }
  190.         }
  191.     if(O){
  192.     twc = split(other,z)
  193.     other = z[1]; lt = length(z[1]);
  194.     for(i=2;i<=twc;i++) {
  195.         if(lt + length(z[i]) >= 55) {sc = "\n\t\t";lt = 0;}
  196.         else sc = " ";
  197.         other = other sc z[i]
  198.         lt += length(z[i]) + 1
  199.         }
  200.         }
  201.     if(X){
  202.     twc = split(abstr,z)
  203.     abstr = z[1]; lt = length(z[1]);
  204.     for(i=2;i<=twc;i++) {
  205.         if(lt + length(z[i]) >= 55) {sc = "\n\t\t";lt = 0;}
  206.         else sc = " ";
  207.         abstr = abstr sc z[i]
  208.         lt += length(z[i]) + 1
  209.         }
  210.         }
  211.  
  212.     printf "@%s{%s",type,keys
  213.     if(A) {
  214.         printf ",\n\tauthor={%s",authors[1]
  215.         for(i=2;i<=A;i++) printf " and %s",authors[i]
  216.         printf "}"
  217.         }
  218.     if(T) printf ",\n\ttitle={%s}",title
  219.     if(B) printf ",\n\tbooktitle={%s}",book
  220.     if(E) {
  221.         printf ",\n\teditor={%s",editor[1]
  222.         for(i=2;i<=E;i++) printf " and\n\t\t%s",editor[i]
  223.         printf "}"
  224.         }
  225.     if(I) printf ",\n\tpublisher={%s}",publisher
  226.     if(C) printf ",\n\taddress={%s}",city
  227.     if(J) { # substitute the journal abbreviations from the standard styles
  228.         journal = "{ " journal "}"
  229.         # {acmcs} {"ACM Computing Surveys"}
  230.         if ( journal ~ /Comp.* Sur/ ) journal = "acmcs"
  231.         # {acta} {"Acta Informatica"}
  232.         if ( journal ~ /Acta Inf/ ) journal = "acta"
  233.         # {cacm} {"Communications of the ACM"}
  234.         if ( journal ~ /Com.* ACM/ ) journal = "cacm"
  235.         if ( journal ~ /CACM/ ) journal = "cacm"
  236.         # {ibmjrd} {"IBM Journal of Research and Development"}
  237.         if ( journal ~ /IBM J.*R.*D/ ) journal = "ibmjrd"
  238.         # {ibmsj} {"IBM Systems Journal"}
  239.         if ( journal ~ /IBM Sy.*J/ ) journal = "ibmsj"
  240.         # {ieeese} {"IEEE Transactions on Software Engineering"}
  241.         if ( journal ~ /IEEE Tran.*Soft.*Eng/ ) journal = "ieeese"
  242.         # {ieeetc} {"IEEE Transactions on Computers"}
  243.         if ( journal ~ /IEEE Tran.*Computers/ ) journal = "ieeetc"
  244.         # {ieeetcad}
  245.         if ( journal ~ /IEEE Tran.*Comp.*Desig/ ) journal = "ieeetcad"
  246.         # {ipl} {"Information Processing Letters"}
  247.         if ( journal ~ /Inf.*Proc.*Lett/ ) journal = "ipl"
  248.         # {jacm} {"Journal of the ACM"}
  249.         if ( journal ~ /Jou.* ACM/ ) journal = "jacm"
  250.         if ( journal ~ /JACM/ ) journal = "jacm"
  251.         # {jcss} {"Journal of Computer and System Sciences"}
  252.         if ( journal ~ /J.*Comp.*Sys.*Sc/ ) journal = "jcss"
  253.         # {scp} {"Science of Computer Programming"}
  254.         if ( journal ~ /Sc.*Comp.*Prog/ ) journal = "scp"
  255.         # {sicomp} {"SIAM Journal on Computing"}
  256.         if ( journal ~ /SIAM .*Comp/ ) journal = "sicomp"
  257.         # {tocs} {"ACM Transactions on Computer Systems"}
  258.         if ( journal ~ /ACM Tran.*Comp.*Sys/ ) journal = "tocs"
  259.         # {tods} {"ACM Transactions on Database Systems"}
  260.         if ( journal ~ /ACM Tran.*Data.*Sys/ ) journal = "tods"
  261.         # {tog} {"ACM Transactions on Graphics"}
  262.         if ( journal ~ /ACM Tran.*Grap/ ) journal = "tog"
  263.         # {toms} {"ACM Transactions on Mathematical Software"}
  264.         if ( journal ~ /ACM Tran.*Math.*Soft/ ) journal = "toms"
  265.         # {toois} {"ACM Transactions on Office Information Systems"}
  266.         if ( journal ~ /ACM Tran.*Off.*Inf.*Sys/ ) journal = "toois"
  267.         # {toplas} {"ACM Transactions on Programming Languages and Systems"}
  268.         if ( journal ~ /ACM Tran.*Prog.*Lan.*Sys/ ) journal = "toplas"
  269.         # {tcs} {"Theoretical Computer Science"}
  270.         if ( journal ~ /Th.*Comp.*Sci/ ) journal = "tcs"
  271.  
  272.         printf ",\n\tjournal=%s",journal
  273.         }
  274.     if(V) printf ",\n\tvolume={%s}",volume
  275.     if(N) printf ",\n\tnumber={%s}",number
  276.     if(P) printf ",\n\tpages={%s}",pages
  277.     if(O) printf ",\n\tnote={%s}",other
  278.     if(R) printf ",\n\tnumber={%s}",report
  279.     if(S) printf ",\n\tseries={%s}",series
  280.     if(df[1] != "")
  281.     printf ",\n\tmonth={%s}",df[1]
  282.     if(D) printf ",\n\tyear={%s}",df[2]
  283.     if(X) printf ",\n\tannote={%s}",abstr
  284.     if(L) printf ",\n\tkey={%s}",label
  285.     printf "\n}\n\n"
  286.  
  287.     A=0;B=0;C=0;D=0;E=0;F=0;G=0;H=0;I=0;J=0;
  288.     K=0;L=0;M=0;N=0;O=0;P=0;Q=0;R=0;S=0;T=0;
  289.     U=0;V=0;W=0;X=0;Y=0;Z=0;
  290.     type = ""
  291.     book=""
  292.     title = ""
  293.     volume = ""
  294.     city = ""
  295.     date = ""
  296.     publisher = ""
  297.     journal = ""
  298.     number = ""
  299.     other = ""
  300.     page = ""
  301.     report = ""
  302.     series = ""
  303.     toterr +=err
  304.     rx++
  305.     }
  306.     err = 0
  307.     pr = NR
  308.     next
  309.     }
  310.  
  311. /^%T/    {
  312.     T ++; lastx = "T"
  313.     if(T>1){err=1
  314.         print "Two titles: Reference " rx > "ref2bib.errs"
  315.         print title > "ref2bib.errs"
  316.         }
  317.     title = substr($0,4)
  318.     next
  319.     }
  320.  
  321. /^%D/    {
  322.     D ++; lastx = "D"
  323.     if(D>1){err=1
  324.         print "Two dates: Reference " rx > "ref2bib.errs"
  325.         print date > "ref2bib.errs"
  326.         }
  327.     if(($NF<1900)||($NF>=2000)){err=1
  328.         print "Date error? : Reference " rx > "ref2bib.errs"
  329.         }
  330.     date = substr($0,4);
  331.     next
  332.     }
  333.  
  334. /^%P/    {
  335.     P ++; lastx = "P"
  336.     if(P>1){err=1
  337.         print "Two page nos? : Reference " rx > "ref2bib.errs"
  338.         print pages > "ref2bib.errs"
  339.         }
  340.     pages = substr($0,4)
  341.     next
  342.     }
  343.  
  344. /^%J/    {
  345.     J ++; lastx = "J"
  346.     if(J>1){err=1
  347.         print "Two journals: Reference " rx > "ref2bib.errs"
  348.         print journal > "ref2bib.errs"
  349.         }
  350.     journal = substr($0,4)
  351.     next
  352.     }
  353.  
  354. /^%V/    {
  355.     V ++; lastx = "V"
  356.     if(V>1){err=1
  357.         print "Two volumes: Reference " rx > "ref2bib.errs"
  358.         print volume > "ref2bib.errs"
  359.         }
  360.     volume = substr($0,4)
  361.     next
  362.     }
  363.  
  364. /^%N/    {
  365.     N ++; lastx = "N"
  366.     if(N>1){err=1
  367.         print "Two issue numbers: Reference " rx > "ref2bib.errs"
  368.         print number > "ref2bib.errs"
  369.         }
  370.     number = substr($0,4)
  371.     next
  372.     }
  373.  
  374. /^[^%]/    {
  375.     if( lastx == "A") authors[A] = authors[A]  " " $0
  376.     if( lastx == "B") book = book " " $0
  377.     if( lastx == "C") city = city " " $0
  378.     if( lastx == "D") date = date " " $0
  379.     if( lastx == "E") editor[E] = editor[E] " " $0
  380.     if( lastx == "I") publisher = publisher " " $0
  381.     if( lastx == "J") journal = journal " " $0
  382.     if( lastx == "L") label = label " " $0
  383.     if( lastx == "N") number = number " " $0
  384.     if( lastx == "O") other = other " " $0
  385.     if( lastx == "P") pages = pages " " $0
  386.     if( lastx == "R") report = report " " $0
  387.     if( lastx == "S") series = series " " $0
  388.     if( lastx == "T") title = title " " $0
  389.     if( lastx == "V") volume = volume " " $0
  390.     if( lastx == "X") abstr = abstr " " $0
  391.     next
  392.     }
  393.  
  394. /^%C/    {
  395.     C ++; lastx = "C"
  396.     if(C>1){err=1
  397.         print "Two cities: Reference " rx > "ref2bib.errs"
  398.         print city > "ref2bib.errs"
  399.         print " 2 cities " FILENAME, pr+1, NR > "ref2bib.errs"
  400.         }
  401.     city = substr($0,4)
  402.     next
  403.     }
  404.  
  405. /^%I/    {
  406.     I ++; lastx = "I"
  407.     if(I>1){err=1
  408.         print "Two publishers: Reference " rx > "ref2bib.errs"
  409.         print publisher > "ref2bib.errs"
  410.         }
  411.     publisher = substr($0,4)
  412.     next
  413.     }
  414.  
  415. /^%B/    {
  416.     B ++; lastx = "B"
  417.     if(B>1){err=1
  418.         print "Two books: Reference " rx > "ref2bib.errs"
  419.         print book > "ref2bib.errs"
  420.         }
  421.     book = substr($0,4)
  422.     next
  423.     }
  424.  
  425. /^%E/    {    # this really deals with 'bib' format
  426.         # refer only allows one %E fielsd, so we ought to
  427.         # split it somehow
  428.     E ++; lastx = "E"
  429.     editor[E] = substr($0,4)
  430.     next
  431.     }
  432.  
  433. /^%[^ABCDEIJKLNOPRSTVX]/    {
  434.         F ++; lastx = "F"; # should not get these
  435.         print "Unexpected flag: Reference " rx > "ref2bib.errs"
  436.         print $0 > "ref2bib.errs"
  437.         err = 1
  438.         next
  439.     }
  440.  
  441. /^%O/    {
  442.     O ++; lastx = "O"
  443.     if(O>1){err=1
  444.         print "Two others: Reference " rx > "ref2bib.errs"
  445.         print other > "ref2bib.errs"
  446.         }
  447.     other = substr($0,4)
  448.     next
  449.     }
  450.  
  451. /^%S/    {
  452.     S ++; lastx = "S"
  453.     if(S>1){err=1
  454.         print "Two series: Reference " rx > "ref2bib.errs"
  455.         print series > "ref2bib.errs"
  456.         }
  457.     series = substr($0,4)
  458.     next
  459.     }
  460.  
  461. /^%R/    {
  462.     R ++; lastx = "R"
  463.     if(R>1){err=1
  464.         print "Two reports: Reference " rx > "ref2bib.errs"
  465.         print report > "ref2bib.errs"
  466.         }
  467.     report = substr($0,4)
  468.     next
  469.     }
  470.  
  471. /^%X/    {
  472.     X ++; lastx = "X"
  473.     abstr = substr($0,4)
  474.     if(X>1){err=1
  475.         print "Two abstracts: Reference " rx > "ref2bib.errs"
  476.         }
  477.     next
  478.     }
  479.  
  480. /^%K/    {
  481.     lastx = "K"
  482.     next
  483.     }
  484. END    {
  485.     print refs " references" > "ref2bib.errs"
  486.     if(toterr) print toterr " erroneous" > "ref2bib.errs"
  487.     if(conf) print conf " conference papers" > "ref2bib.errs"
  488.     if(jour) print jour " journal articles" > "ref2bib.errs"
  489.     if(wholebook) print wholebook " books" > "ref2bib.errs"
  490.     if(totB) print totB " book articles" > "ref2bib.errs"
  491.     if(reps) print reps " reports" > "ref2bib.errs"
  492.     if(unclass) print unclass " Unclassified" > "ref2bib.errs"
  493.     if(totO) print totO " have additional information." > "ref2bib.errs"
  494.     if(totK) print totK " have additional keywords." > "ref2bib.errs"
  495.     if(totX) print totX " have abstracts/commentaries." > "ref2bib.errs"
  496.     print totA " authors" > "ref2bib.errs"
  497.     for(i=0;i<=MaxA;i++)if(acnt[i]){
  498.         print i, " authors ", acnt[i] > "ref2bib.errs"
  499.         av += i*acnt[i]
  500.         }
  501.     print "Average ", av/refs > "ref2bib.errs"
  502.     print totT " titles" > "ref2bib.errs"
  503.     print "Key frequencies" > "ref2bib.errs"
  504.     for(k in keyused) print k, keyused[k] > "ref2bib.errs"
  505.  
  506.     }
  507.